package com.dwsoft.word;

import com.dwsoft.mapper.IDataKpiMapper;
import com.dwsoft.xml.XmlNode;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import word.w2004.Document2004;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author sqw
 * @version 1.0
 * @description TODO
 * @ClassName ParagraphStrategy
 * @Date 2020/8/5
 * @since jdk1.8
 */
@Data
public abstract class WElement<T>{

    @Autowired
    protected ElementContext paragraphContext;

    @Value("${user.home}")
    protected String sysUserHome;

    protected static final String FILE_DIR="wordTmpFile";

    @Autowired
    protected IDataKpiMapper dataKpiMapper;

    protected Map<String,String> params;

    private int breakTimes=0;

    public abstract void instance(T xmlNode, Document2004 document) throws Exception;

    protected String searcheKpiParam(String attr){
        Pattern p = Pattern.compile("\\$\\{.*\\}");
        Matcher m = p.matcher(attr);
        if(m.find()){
            return m.group();
        }
        return "";
    }

    public void addChild(List<XmlNode> childrens,XmlNode xmlNode,Document2004 document) throws Exception {
        for(int i=0;i<childrens.size();i++){
            XmlNode children = childrens.get(i);
            children.setIndex(xmlNode.getIndex()+"."+(i+1));
            String className=children.getClass().getSimpleName().substring(1).toLowerCase();
            paragraphContext.getBean(className).instance(children,document);
        }
    }

}
